$(function () {
   var showUrl = '/backstage/person/showPersonInfo';
   var type = getQueryString('admin');
   if (type){
       $('#show').hide();
   }else {
       $('#edit').hide();
       $('#update').hide();
   }
   var userId = getQueryString('userId');

   showPerson();

   function showPerson() {
        var url = showUrl+'?userId='+userId;
        $.ajax({
            url:url,
            type:'GET',
            contentType:false,
            processData:false,
            cache:false,
            // beforeSend: function(request){
            //     request.setRequestHeader("Token",localStorage.getItem('token'));
            // },
            success: function (data) {

                if (data.success){
                    console.log(data);
                    $('#headImg').attr('src',data.data.headImg);
                    $('#L_phone').val(data.data.phone);
                    $('#L_username').val(data.data.username);
                    $('#L_gender').val(data.data.gender);
                    $('#L_time').val(new Date(data.data.createTime).Format("yyyy-MM-dd"));
                    $(" select option[value='"+data.data.role+"']").attr("selected","selected");
                    $('#role').val(data.data.role);
                    $('select #role').find('option[value="'+data.data.role+'"]').attr('selected','selected');
                } else {
                    layer.msg(data.message,{icon: 5,time:1000});
                }
            }
        })
   }

    $('#submit').click(function () {
        var role = $('#role').val();
        var url = '/backstage/person/updateRole?userId='+userId+'&role='+role;
        $.ajax({
            url:url,
            type:'POST',
            contentType:false,
            processData:false,
            cache:false,
            beforeSend: function(request){
                request.setRequestHeader("Token",localStorage.getItem('token'));
            },
            success: function (data) {
                alert(data.message);
                if (data.success){
                    showPerson();
                }
            }
        })
    })
});
